package practise.alogrithm.hundredInterviewQuestions;

/**
 * convert a string to integer, please not the overflow and the illegal charactors.
 */
public class ConvertStrToInt {

    public static int convertStringToInt(String str){
        int result=0;
        if(str.length()==0){
            throw new IllegalArgumentException("str is empty");
        }
        for(char ch : str.toCharArray()){
            if(ch<'0'|| ch>'9'){
                throw new IllegalArgumentException("String contain illegal char:"+ch);
            }
            if(result>Integer.MAX_VALUE/10||(result==Integer.MAX_VALUE/10&&(ch-'0'>(Integer.MAX_VALUE%10)))){
                throw new IllegalArgumentException("String exceed the max value of Integer.");
            }
            result=(result*10)+ch-'0';
        }
        return result;
    }
    public static void main(String[] arg){
        System.out.println("result:"+convertStringToInt("12345678901234567890"));
    }
}
